/*
 * XTableColumn.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.table;

import java.util.Comparator;

import javax.swing.table.TableColumn;

import ca.odell.glazedlists.GlazedLists;

/**
 * 
 * 
 * @author Devon Carew
 */
public abstract class XTableColumn
	extends TableColumn
{
	private boolean 	editable = false;
	private boolean 	filterable = true;
	private Comparator	columnComparator;
	private Class		columnClass;
	
	public XTableColumn()
	{
		this(null);
	}
	
	public XTableColumn(String columnName)
	{
		this(columnName, (Comparator)null);
	}
	
	public XTableColumn(String columnName, Comparator columnComparator)
	{
		setHeaderValue(columnName);
		setColumnComparator(columnComparator);
	}
	
	public XTableColumn(String columnName, Class columnClass)
	{
		setHeaderValue(columnName);
		
		this.columnClass = columnClass;
	}
	
	public XTableColumn(String columnName, Class columnClass, int startingWidth)
	{
		this(columnName, columnClass);
		
		setPreferredWidth(startingWidth);
	}
	
	public Class getColumnClass()
	{
		return columnClass;
	}

	public abstract Object getValue(Object rowObject);
	
	public Object getSortValue(Object rowObject)
	{
		return getValue(rowObject);
	}

	public void setValue(Object rowObject, Object newValue)
	{
		
	}

	public boolean isEditable()
	{
		return editable;
	}

	public void setEditable(boolean editable)
	{
		this.editable = editable;
	}

	public Comparator getColumnComparator()
	{
		if (columnComparator != null)
			return columnComparator;
		
		if (getColumnClass() != null && Comparable.class.isAssignableFrom(getColumnClass()))
			columnComparator = GlazedLists.comparableComparator();
		
		return columnComparator;
	}

	public void setColumnComparator(Comparator columnComparator)
	{
		this.columnComparator = columnComparator;
	}

	public boolean isSortable()
	{
		return getColumnComparator() != null;
	}

	public boolean isFilterable()
	{
		return filterable;
	}
	
	public void setFilterable(boolean filterable)
	{
		this.filterable = filterable;
	}
	
	public String getFilterText(Object rowObject)
	{
		Object object = getValue(rowObject);
		
		if (object == null)
			return null;
		
		return object instanceof String ? (String)object : object.toString();
	}
	
}
